package com.atguigu.spzx.cart.controller;

import com.atguigu.spzx.cart.service.CartService;
import com.atguigu.spzx.model.entity.h5.CartInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: CartController
 * @Package: com.atguigu.spzx.cart.controller
 * @Description 购物车
 * @Author 孤妤
 * @Date: 2023/12/19 20:37
 */
@Tag(name = "购物车")
@RestController
@RequestMapping("/api/order/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @GetMapping("/auth/addToCart/{skuId}/{skuNum}")
    @Operation(summary = "添加到购物车")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum){
        cartService.addTocart(skuId,skuNum);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/auth/cartList")
    @Operation(summary = "查询购物车商品列表")
    public Result findCartList(){
        List<CartInfo> cartInfoList = cartService.findCartList();
        return Result.build(cartInfoList,ResultCodeEnum.SUCCESS);
    }
    @DeleteMapping("/auth/deleteCart/{skuId}")
    @Operation(summary = "移除购物车商品")
    public Result removeCartBySkuId(@PathVariable Long skuId){
        cartService.removeCartBySkuId(skuId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/auth/checkCart/{skuId}/{isChecked}")
    @Operation(summary = "更新购物车中的状态")
    public Result changeCartIsCheckedBySkuId(@PathVariable Long skuId,
                                             @PathVariable Integer isChecked){
        cartService.changeCartIsCheckedBySkuId(skuId,isChecked);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/auth/allCheckCart/{isChecked}")
    @Operation(summary = "全选商品或去除全选")
    public Result changeAllCartIsCheckedBySkuId(@PathVariable Integer isChecked){
        cartService.changeAllCartIsCheckedBySkuId(isChecked);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/auth/clearCart")
    @Operation(summary = "清空购物车")
    public Result changeAllCart(){
        cartService.changeAllCart();
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "获取用户的购物项")
    @GetMapping("/auth/getAllCkecked")
    public List<CartInfo> findCart(){
        return cartService.findAllCartIsChecked();
    }
    @Operation(summary = "用户支付完毕清空购物车")
    @GetMapping(value = "/auth/deleteChecked")
    public void deleteChecked() {
        cartService.deleteChecked() ;
    }
}
